using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Data.Linq;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Web;
using HtmlAgilityPack;
using System.Linq.Expressions;

namespace WapCrawlerLib
{
    partial class TestLinq
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();
        public TestLinq Insert1(TestLinq objTestLinq)
        {
            dc.TestLinqs.InsertOnSubmit(objTestLinq);
            dc.SubmitChanges();
            return objTestLinq;
        }

        public TestLinq Insert2()
        {
            dc.TestLinqs.InsertOnSubmit(this);
            dc.SubmitChanges();
            return this;
        }

        public TestLinq Insert3(string name, int value)
        {
            TestLinq objTestLinq = new TestLinq();
            objTestLinq.name = name;
            objTestLinq.value = value;
            dc.TestLinqs.InsertOnSubmit(objTestLinq);
            dc.SubmitChanges();
            return objTestLinq;
        }

        public TestLinq Insert4(string name, int value)
        {
            this.name = name;
            this.value = value;
            dc.TestLinqs.InsertOnSubmit(this);
            dc.SubmitChanges();
            return this;
        }

        public TestLinq Get(int id)
        {
            return dc.TestLinqs.Single(p => p.id == id);
        }

        public TestLinq Update()
        {
            dc.SubmitChanges();
            return this;
        }

        //public TestLinq Update()
        //{
        //    TestLinq objTestLinq = dc.TestLinqs.Single(p => p.id == this.id);

        //    dc.SubmitChanges();
        //    return this;
        //}

    }

    partial class FileCrawler 
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        //public bool IsExist(string url)
        //{
        //    bool result = false;
        //    var query = from p in dc.FileCrawlers
        //                where p.fileUrl == url
        //                select p;
        //    if (query.Count() > 0)
        //        result = true;
        //    return result;
        //}

        //public FileCrawler Insert(FileCrawler objFileCrawler)
        // {
        //     dc.FileCrawlers.InsertOnSubmit(objFileCrawler);
        //     dc.SubmitChanges();
        //     return objFileCrawler;
        // }

        //public FileCrawler Update()
        //{
        //    FileCrawler file = dc.FileCrawlers.Single(p => p.fileCrawlerId == this.fileCrawlerId);
        //    file.fileUrl = this.fileUrl;
        //    file.fileName = this.fileName;
        //    dc.SubmitChanges();
        //    return file;
        //}
    }

    partial class ImageCrawler
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        //public bool IsExist(string url)
        //{
        //    bool result = false;
        //    var query = from p in dc.ImageCrawlers
        //                where p.imageUrl == url
        //                select p;
        //    if (query.Count() > 0)
        //        result = true;
        //    return result;
        //}

        //public ImageCrawler Insert()
        //{
        //    dc.ImageCrawlers.InsertOnSubmit(this);
        //    dc.SubmitChanges();
        //    return this;
        //}

        //public ImageCrawler Update()
        //{
        //    ImageCrawler img = dc.ImageCrawlers.Single(p => p.imageCrawlerId == this.imageCrawlerId);
        //    img.imageUrl = this.imageUrl;
        //    dc.SubmitChanges();
        //    return img;
        //}
    }

    partial class TopicPostCrawler
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        //public bool IsExist(int topicId, int postId)
        //{
        //    bool result = false;
        //    var query = from p in dc.TopicPostCrawlers
        //                where p.topicCrawlerId == topicId &&
        //                p.postCrawlerId == postId
        //                select p;
        //    if (query.Count() > 0)
        //        result = true;
        //    return result;
        //}

        //public TopicPostCrawler Insert()
        //{
        //    dc.TopicPostCrawlers.InsertOnSubmit(this);
        //    dc.SubmitChanges();
        //    return this;
        //}
    }

    partial class TopicCrawler
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        //public bool IsExist(int value)
        //{
        //    bool result = false;
        //    var query = from p in dc.TopicCrawlers
        //                where p.topicValue == value
        //                select p;
        //    if (query.Count() > 0)
        //        result = true;
        //    return result;
        //}

        //public TopicCrawler Get(int value)
        //{
        //    return dc.TopicCrawlers.Single(q => q.topicValue == value);
        //}

        //public TopicCrawler Insert(TopicCrawler objTopicCrawler)
        //{
        //    dc.TopicCrawlers.InsertOnSubmit(objTopicCrawler);
        //    dc.SubmitChanges();
        //    return objTopicCrawler;
        //}
    }

    partial class WapCrawler
    {
        WapDataClassesDataContext dc = new WapDataClassesDataContext();

        public WapCrawler GetWapCrawler(int id)
        {
            return dc.WapCrawlers.Single(q => q.wapCrawlerId == id);
        }

        public void UpdateRecent(string recentUrl)
        {
            WapCrawler objWC = dc.WapCrawlers.Single(q => q.wapCrawlerId == this.wapCrawlerId);
            objWC.recentCrawlerUrl = recentUrl;
            dc.SubmitChanges();
        }

        public void Update()
        {
            //WapCrawler objWC = dc.WapCrawlers.Single(q => q.wapCrawlerId == this.wapCrawlerId);
            //objWC.recentCrawlerUrl = recentUrl;
            dc.SubmitChanges();
        }
    }

    partial class PostCrawler
    {
        //WapDataClassesDataContext dc = new WapDataClassesDataContext();
        
        //public bool IsExist(string url)
        //{
        //    bool result = false;
        //    var query = from p in dc.PostCrawlers
        //                where p.postUrl == url
        //                select p;
        //    if (query.Count() > 0)
        //        result = true;
        //    return result;
        //}

        //public PostCrawler Get(string url)
        //{
        //    return dc.PostCrawlers.Single(q => q.postUrl == url);
        //}

        //public PostCrawler Insert(string title, string url, string head, string content)
        //{
        //    PostCrawler post = new PostCrawler();
        //    post.postTitle = title;
        //    post.postUrl = url;
        //    post.postHead = head;
        //    post.postContent = content;
        //    post = Insert(post);
        //    return post;
        //}

        //public PostCrawler Insert(PostCrawler objPost)
        //{
        //    //if (objPost.postId != null)
        //    //    return objPost = Insert(objPost.postTitle, 
        //    //        objPost.postUrl, 
        //    //        objPost.postHead, 
        //    //        objPost.postContent, 
        //    //        objPost.FileCrawlers, 
        //    //        objPost,ImageCrawlers);

        //    PostCrawler post = new PostCrawler();
            
        //    post.postTitle = objPost.postTitle;
        //    post.postUrl = objPost.postUrl;
        //    post.postHead = objPost.postHead;
        //    post.postContent = objPost.postContent;
        //    post.FileCrawlers = objPost.FileCrawlers;
        //    post.ImageCrawlers = objPost.ImageCrawlers;
        //    dc.PostCrawlers.InsertOnSubmit(post);
        //    dc.SubmitChanges();
        //    return post;
        //    return objPost;
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="title"></param>
        ///// <param name="url"></param>
        ///// <param name="head"></param>
        ///// <param name="content"></param>
        ///// <param name="fileCrawlers"></param>
        ///// <param name="imageCrawlers"></param>
        ///// <returns></returns>
        //public PostCrawler Insert(string title, string url, string head, string content, EntitySet<FileCrawler> fileCrawlers, EntitySet<ImageCrawler> imageCrawlers)
        //{
        //    PostCrawler post = new PostCrawler();
        //    post.postTitle = title;
        //    post.postUrl = url;
        //    post.postHead = head;
        //    post.postContent = content;
        //    post.FileCrawlers = fileCrawlers;
        //    post.ImageCrawlers = imageCrawlers;
        //    dc.PostCrawlers.InsertOnSubmit(post);
        //    dc.SubmitChanges();
        //    return post;
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public PostCrawler Insert()
        //{
        //    dc.PostCrawlers.InsertOnSubmit(this);
        //    dc.SubmitChanges();
        //    return this;
        //}

        //public PostCrawler Update()
        //{
        //    //PostCrawler post = dc.PostCrawlers.Single(p => p.postCrawlerId == this.postCrawlerId);
        //    //post.postId = this.postId;
        //    //post.postTitle = this._postTitle;
        //    //post.postUrl = this._postUrl;
        //    //post.postHead = this._postHead;
        //    //post.postContent = this._postContent;
        //    //post.postDate = this._postDate;
        //    dc.SubmitChanges();
        //    return this;
        //}
    }
}
